<?php
	namespace ampleharvest\code\search;
	use\ampleharvest\code;

	require_once dirname(dirname(dirname(__FILE__))) . '/config/cConfig.php';
	$codeConfig = new \ampleharvest\code\cConfig();

	class cFacets
	{
		const C_INT_SOLR_TYPE_REAPER = 0;
		const C_INT_SOLR_TYPE_GROUNDEFFECT = 1;
		const C_INT_SOLR_TYPE_WIKI = 2;

		private $m_obj_Solr = null;
		private $m_str_Prefix = "";
		private $m_str_BaseQuery = "*:*";
		private $m_str_Facet = "";
		private $m_int_SolrType = "";

		public function set_Prefix($str_Prefix)
		{
			$this->m_str_Prefix = $str_Prefix;
		}

		public function set_Facet($str_Facet)
		{
			$this->m_str_Facet = $str_Facet;
		}

		public function set_BaseQuery($str_BaseQuery)
		{
			$this->m_str_BaseQuery = $str_BaseQuery;
		}

		public function set_SolrType($int_SolrType)
		{
			$this->m_int_SolrType = $int_SolrType;

			switch ($this->m_int_SolrType)
			{
				case self::C_INT_SOLR_TYPE_REAPER:
					require_once $codeConfig->C_STR_SOLR_PATH . '/Classes/cReaper_Solr.php';
					$this->m_obj_Solr = new \cReaper_Solr();
					break;
				case self::C_INT_SOLR_TYPE_GROUNDEFFECT:
					require_once $codeConfig->C_STR_SOLR_PATH . '/Classes/cGE_Solr.php';
					$this->m_obj_Solr = new \cGE_Solr();
					break;
				case self::C_INT_SOLR_TYPE_WIKI:
					require_once $codeConfig->C_STR_SOLR_PATH . '/Classes/cWiki_Solr.php';
					$this->m_obj_Solr = new \cWiki_Solr();
					break;
				default:

					break;
			}
		}

		public function get_OutputArray()
		{
			$arr_Output = array();
			if (!$this->m_str_Facet)
			{
				$arr_Output["success"] = false;
				$arr_Output["errors"] = array();
				$arr_Output["errors"]["msg"] = "You have not specified a Facet to search for.";
				return $arr_Output;
			}

			$obj_Response = $this->m_obj_Solr->search(
				$this->m_str_BaseQuery, 
				0,
				0,
				array(
				"rows"			 => 0,
				"facet.field"	 => $this->m_str_Facet,
				"facet.mincount" => 1,
				"facet.prefix"	 => $this->m_str_Prefix,
				"facet"			 => "on"
				)
			);

			if ($obj_Response->getHttpStatus() == 200)
			{
				$arr_Output["terms"] = array();
				//print_r($obj_Response);
				if(isset($obj_Response->facet_counts))
				{
					foreach ($obj_Response->facet_counts->facet_fields as $str_Field)
					{
						foreach ($str_Field as $str_Term => $int_Count)
						{
							$arr_Temp = array();
							if(is_numeric($str_Term))
							{
								$arr_Temp["term"] = (float)$str_Term;
							}
							else
							{
								$arr_Temp["term"] = $str_Term;
							}
							$arr_Temp["count"] = $int_Count;
							$arr_Temp["label"] = $str_Term . " ($int_Count)";
							$arr_Output["terms"][] = $arr_Temp;
	
						}
					}
				}
			}
			else
			{
				$arr_Output["success"] = false;
				$arr_Output["errors"] = array();
				$arr_Output["errors"]["msg"] = $obj_Response->getHttpStatusMessage();
				return $arr_Output;
			}
			return $arr_Output;
		}

		public function get_OutputJSON()
		{
			return json_encode($this->get_OutputArray());
		}
	}
?>